package v1

import (
	"github.com/gin-gonic/gin"
	"manager/bll"
	"manager/model"
	"manager/utils"
)

var UserEbike = &userEbike{}

func init() {
	// 注册路由
	RegisterRouter(UserEbike)
}

type userEbike struct{}

func (a *userEbike) Init(r *gin.RouterGroup) {
	g := r.Group("/user_device")
	{
		/*g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/delete", a.delete)*/
		g.POST("/count", a.count)
		g.POST("/detail", a.detail)
		g.POST("/list", a.list)
		g.POST("/bind_info", a.bindInfo)
		g.POST("/bike_manager", a.bikeManager)
	}

	appG := r.Group("/app/user_device")
	{
		appG.POST("/bike_manager", a.bikeManager)
	}
}

func (a *userEbike) create(c *gin.Context) {
	var (
		in  = &model.UserEbikeCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserEbike.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *userEbike) update(c *gin.Context) {
	var (
		in  = &model.UserEbikeUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserEbike.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *userEbike) list(c *gin.Context) {
	var (
		in  = &model.UserEbikeListRequest{}
		out = &model.UserEbikeListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.UserEbike.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// bindInfo 绑定信息
func (a *userEbike) bindInfo(c *gin.Context) {
	var (
		in  = &model.UserEbikeBindInfoRequest{}
		out = &model.UserEbikeBindInfoResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if rsp, err := bll.UserEbike.List(c.Request.Context(), &model.UserEbikeListRequest{
		Index:   1,
		Size:    -1,
		EbikeID: in.EbikeId,
	}); err != nil {
		c.Error(err)
		return
	} else {
		for i := 0; i < len(rsp.Data); i++ {
			if rsp.Data[i].IsManager == 1 {
				out.EbikeMasterId = rsp.Data[i].UserID
				out.MasterBindTime = rsp.Data[i].CreatedAt
			} else {
				out.ShareUserIds = append(out.ShareUserIds, rsp.Data[i].UserID)
			}
		}
	}

	utils.ResponseOk(c, out)
}

func (a *userEbike) delete(c *gin.Context) {
	var (
		in  = &model.UserEbikeDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.UserEbike.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// detail 详情
func (a *userEbike) detail(c *gin.Context) {
	var (
		in  = &model.UserBikeDetailRequest{}
		out = &model.UserBikeDetailResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.UserEbike.Detail(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

func (a *userEbike) count(c *gin.Context) {
	type TempIn struct {
		Ids []int64 `json:"ids"`
	}
	type EbikeCount struct {
		ID    int64 `json:"id"`
		Count int64 `json:"count"`
	}
	var (
		err error
		in  TempIn
		out = make([]EbikeCount, 0)
	)

	if err = c.ShouldBindJSON(&in); err != nil {
		c.Error(err)
		return
	}
	out = append(out, EbikeCount{
		ID:    35,
		Count: 100,
	})

	utils.ResponseOk(c, out)

}

// detail 详情
func (a *userEbike) bikeManager(c *gin.Context) {
	var (
		in  = &model.BikeManagerRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	// 获取车辆id
	bikeId, err := bll.CacheBike.GetBikeIdBySn(in.Sn)
	if err != nil {
		c.Error(err)
		return
	}

	// 获取车辆管理员
	userId := bll.UserEbike.GetManagerByBikeId(bikeId)

	utils.ResponseOk(c, &model.BikeManagerResponse{
		UserId: userId,
		Sn:     in.Sn,
	})
}
